<?php

/**
 * Class Core_Mongo
 * @package Core
 */
class Core_Mongo {

    protected static $_stack = array();

    private function __construct() {

    }

    /**
     * @param string $name
     * @return Mongo
     */
    public static function getInstance($name = 'default') {
        if (empty(self::$_stack[$name])) {
            self::$_stack[$name] = new MongoClient(Core_Config::get("mongo.{$name}.server"), Core_Config::get("mongo.{$name}.options", array()));
        }
        return self::$_stack[$name];
    }

    /**
     * @param string $db
     * @param string $name
     * @return MongoDB
     */
    public static function getDB($db = '', $name = 'default') {
        $db || $db = Core_Config::get("mongo.{$name}.database");
        return self::getInstance($name)->selectDB($db);
    }

    /**
     * @param string $collection
     * @param string $db
     * @param string $name
     * @return MongoCollection
     */
    public static function getCollection($collection, $db = '', $name = 'default') {
        Core_Stats::$mongoQueryCnt++;
        return self::getDB($db, $name)->selectCollection($collection);
    }

}
